#ifndef CHILD_CREAT_REQ_LIST
#define CHILD_CREAT_REQ_LIST

#include <sys/stat.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <errno.h>
#include <fcntl.h>
#include "../DataStructures/FifoException.h"
#include "../CommunicationModule/DlmComm.h"
#include "../LoggerModule/Logger.h"
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

/**
 * Main class of ChildrenCreationModule. It's responsible for creating a FIFO,
 * waiting for process creation requests and serving them.
 */
class ChildCreationRequestListener
{
private:

	std::string* fifoName;

	char readBuffer[200];

	int fifoDescriptor;

	int dlm_pipe;

	int info_pipe;

	static DlmComm* communicationModule;

	struct sigaction action;

	ChildCreationRequestListener();

	static void sigchldHandler(int signum)
	{
		pid_t pid;

		pid = wait(NULL);
		Logger::getInstance().logDlm(Logger::INFO, "[CHILD_CREATION_MODULE] Child process terminated: %d.", pid);
		if (communicationModule != NULL)
			communicationModule->clientUnregister(pid);
	};

public:
	ChildCreationRequestListener(DlmComm* communicationModule, int dlm_pipe, int info_pipe);

	~ChildCreationRequestListener();

	void initializeFifo();

	void* listen();
};

#endif
